package com.example.wangzh.developkit.control;

import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.adapter.CountryAdapter;
import com.example.wangzh.developkit.base.BaseActivity;
import com.example.wangzh.developkit.model.CountryEntity;
import com.example.wangzh.developkit.utils.UIUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import me.yokeyword.indexablerv.IndexableAdapter;
import me.yokeyword.indexablerv.IndexableLayout;
import me.yokeyword.indexablerv.SimpleHeaderAdapter;

/**
 * Created by wangzh on 2017/10/31.
 */

public class IndexableListActivity extends BaseActivity {
    @BindView(R.id.indexableLayout)
    IndexableLayout indexableLayout;


    private List<CountryEntity> mDatas;
    private SimpleHeaderAdapter mHotCountryAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_indexable_list;
    }

    @Override
    protected void initView() {
        initIndexable();
    }

    private void initIndexable() {
        indexableLayout.setLayoutManager(new LinearLayoutManager(this));
//      indexableLayout.setLayoutManager(new GridLayoutManager(this, 2));

        // 多音字处理
//      Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance(this)));

        // 添加自定义多音字词典
//      Pinyin.init(Pinyin.newConfig()
//                .with(new PinyinMapDict() {
//                    @Override
//                    public Map<String, String[]> mapping() {
//                        HashMap<String, String[]> map = new HashMap<String, String[]>();
//                        map.put("重庆",  new String[]{"CHONG", "QING"});
//                        return map;
//                    }
//                }));

        // 快速排序。  排序规则设置为：只按首字母  （默认全拼音排序）  效率很高，是默认的10倍左右。  按需开启～
        indexableLayout.setCompareMode(IndexableLayout.MODE_FAST);
        // 自定义排序规则
//        indexableLayout.setComparator(new Comparator<EntityWrapper<CityEntity>>() {
//            @Override
//            public int compare(EntityWrapper<CityEntity> lhs, EntityWrapper<CityEntity> rhs) {
//                return lhs.getPinyin().compareTo(rhs.getPinyin());
//            }
//        });

        // setAdapter
        CountryAdapter adapter = new CountryAdapter(this);
        indexableLayout.setAdapter(adapter);

        // set Datas
        mDatas = initDatas();
        adapter.setDatas(mDatas);

        // set Center OverlayView
//      indexableLayout.setOverlayStyle_Center();
        indexableLayout.setOverlayStyle_MaterialDesign(R.color.yellow);

        // set Listener
        adapter.setOnItemContentClickListener(new IndexableAdapter.OnItemContentClickListener<CountryEntity>() {
            @Override
            public void onItemClick(View v, int originalPosition, int currentPosition, CountryEntity entity) {
                if (originalPosition >= 0) {
                    UIUtils.showToast("选中:" + entity.getName() +
                            "  当前位置:" + currentPosition + "  原始所在数组位置:" + originalPosition);
                } else {
                    UIUtils.showToast("选中Header:" + entity.getName() +
                            "  当前位置:" + currentPosition);
                }
            }
        });

        adapter.setOnItemTitleClickListener(new IndexableAdapter.OnItemTitleClickListener() {
            @Override
            public void onItemClick(View v, int currentPosition, String indexTitle) {
                UIUtils.showToast("选中:" + indexTitle +
                        "  当前位置:" + currentPosition);
            }
        });

        mHotCountryAdapter = new SimpleHeaderAdapter<>(adapter, "热", "热门国家", iniyHotCountryDatas());
        // 热门城市
        indexableLayout.addHeaderAdapter(mHotCountryAdapter);
    }

    private List<CountryEntity> iniyHotCountryDatas() {
        List<CountryEntity> list = new ArrayList<>();
        list.add(new CountryEntity("中国"));
        list.add(new CountryEntity("韩国"));
        list.add(new CountryEntity("日本"));
        list.add(new CountryEntity("新加坡"));
        return list;
    }

    private List<CountryEntity> initDatas() {
        List<CountryEntity> list = new ArrayList<>();
        List<String> cityStrings = Arrays.asList(getResources().getStringArray(R.array.countries));
        for (String item : cityStrings) {
            CountryEntity cityEntity = new CountryEntity();
            cityEntity.setName(item);
            list.add(cityEntity);
        }
        return list;
    }
}
